package com.example.common;

/**
 * 配置返回数据的格式
 * */
public class Result<T> {
    private String code; // 结果码，比如请求成功返回0，失败返回-1
    private String msg;  // 请求成功或失败，返回的提示信息
    private T data;      // 实际接口返回的数据

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }

    public Result() { }
    public Result(T data) {
        this.data = data;
    }

    // 请求成功，无返回值
    public static Result success() {
        Result result = new Result<>();
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }

    // 请求成功，有返回值
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>(data);
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }

    // 请求失败
    public static Result error(String code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}
